package vsy.buzzwordbingo.sort;

import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import vsy.buzzwordbingo.sort.OrderedComparator.Order;

@SuppressWarnings("rawtypes")
public class ComparatorProvider {
	private Map<Class, Map<String, Map<String, Comparator>>> comparators;

	private String descending;
	private String ascending;

	@SuppressWarnings("unchecked")
	public <T> Comparator<T> getComparator(Class<T> c, String sort, String order) {
		return comparators.get(c).get(sort).get(order);
	}

	public void setAscending(String asc) {
		this.ascending = asc;
	}

	public void setDescending(String desc) {
		this.descending = desc;
	}

	public void setComparators(Map<String, Map<String, String>> comparatorDefs)
			throws ClassNotFoundException, IllegalArgumentException,
			SecurityException, InstantiationException, IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		comparators = new HashMap<Class, Map<String, Map<String, Comparator>>>();
		for (String className : comparatorDefs.keySet()) {
			Class<?> c = Class.forName(className);
			Map<String, String> comparatorDefsForClass = comparatorDefs.get(className);
			Map<String, Map<String, Comparator>> comparatorsForClass = new HashMap<String, Map<String, Comparator>>();
			for (String attribute : comparatorDefsForClass.keySet()) {
				String compClassName = comparatorDefsForClass.get(attribute);
				Class<?> comparatorClass = Class.forName(compClassName);
				Map<String, Comparator> comps = new HashMap<String, Comparator>();
				Comparator ascComparator = (Comparator) comparatorClass
						.getConstructor(Order.class).newInstance(
								Order.ASCENDING);
				Comparator descComparator = (Comparator) comparatorClass
						.getConstructor(Order.class).newInstance(
								Order.DESCENDING);
				comps.put(ascending, ascComparator);
				comps.put(descending, descComparator);
				comparatorsForClass.put(attribute, comps);
			}
			comparators.put(c, comparatorsForClass);
		}
	}
}
